package com.css.fxfzfxqh.zcpt.sys.service.impl;

import com.css.fxfzfxqh.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class SysAreaCacheService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    SysAreaService sysAreaService;
    public static final String VALUE_KEY = "fxqh:area:";

    public String getAreaNameByAreaId(String areaId) {
        if (StringUtils.isBlank(areaId)) {
            return null;
        }
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String name = ops.get(VALUE_KEY + areaId);
        if (StringUtils.isBlank(name)) {
            name = sysAreaService.getAreaNameByAreaId(areaId);
            log.debug("SysAreaCacheService-getAreaNameByAreaId:{}", name);
        }
        ops.set(VALUE_KEY + areaId, name);
        redisTemplate.expire(VALUE_KEY + areaId, 5L, TimeUnit.MINUTES);
        return name;
    }
}
